<?php 
include('../../Connections/KRAPPERCON.php');
$oper = $_POST['oper'];
$newnro = $_POST['solped'];


/*$fecdoc = '2013-02-20';
$fecent = '2013-02-20';
$codprv = '10004';
$cladoc = 'OC_OK';
$items = '178010C010|12.32|3|Filtro de Aire1 #178010C010|10.45|7|Filtro de Aire2#';*/
//echo $oper;

/*
`sp_saveSolped`(
nro decimal(10,0),fecdoc date, fecent date,codprv numeric(10,0),
 cldoc char(5),moned char(4),items varchar(500), usrid char(15), ref varchar(20),txt varchar(45))
*/

 
	if ($oper == 'upd'){
$fecdoc = $_POST['t_fecdoc'];
$fecent = $_POST['t_fecent'];
$codprv = $_POST['cb_proveedor'];
$cladoc = $_POST['cb_sp_cdoc'];
$ref = $_POST['t_sp_ref'];
$txt = $_POST['t_sp_txt'];
$mon = $_POST['cb_sp_mon'];
$items = $_POST['items'];
	
		$sql = "call sp_saveSolped('".$newnro."','".$fecdoc."','".$fecent."','".$codprv."','".$cladoc."','".$mon."','".$items."','".$user_now."','".$ref."','".$txt."');";
		//doEcho($sql);
		//exit;
		$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);
		if ($res)
		if ($row = $res->fetch_assoc()){
			$newnro = $row['newnro'];
			if ($newnro > 0) {
				echo '<script type="text/javascript">$("#sp_nrosp").html("'.$newnro.'");updItemSP();noEditSP();showOk("Se ha '.$row['tip_msg'].' la Orden '.$newnro.'");
						
					</script>';
			}
			else {
				echo '<script type="text/javascript">showOk("Error al Generar la OC\n'.$row['msg_err'].'\n'.$row['pos_doc'].'");</script>';
			}
		}
	
	}
	elseif ($oper == 'take'){
		/*
		newrow.find('.clsSolpID').html(obj[x].spID);
					newrow.find('.clsSolpIt').html(obj[x].spItm);
					newrow.find('.clsCodOc').html(obj[x].code);					
					newrow.find('.clsOCAlm').val(obj[x].alm);
					newrow.find('.clsTxtOc').val(obj[x].txt);	
					newrow.find('.clsQtyOc').val(obj[x].qty);	 // Aqui mosrrar solo lo pendient
					newrow.find('.clsCstUni').val(obj[x].preun);	
					newrow.find('.clsQtyUM').val(obj[x].unm);
					newrow.find('.clsBruOc').html(obj[x].brut);
		*/
		$sql = "call sp_takeSP('".$newnro."');";
		//doEcho($sql);
		$json = "[";
		$i = 0;
		$res = $mysqli->query($sql);
		if ($res){
		
			while($row=$res->fetch_assoc()){
			$i++;
				$item = "{spID :'".$row['SolPedID']."', spItm : '".$row['SolPedItm']."', code:'".$row['ArticuloID']."', alm:'".$row['AlmacenID']."', txt:'".$row['ArticuloTxt']."', qty:".$row['pendiente'].", preun:'".$row['PreUM']."', unm: '".$row['UniMed']."', brut:'".$row['PreBru']."', neto:'".$row['PreNet']."',facUM:'".$row['FactorUM']."'}";
				if ($i>1)
					$json = $json .",". $item;
				else
					$json = $json . $item;
			}
			
				$mysqli->check_result();
			
			$res->close();
		}
		
		$json=$json."]";
		echo $json;
	}

?>